package com.xjmall.service.impl;

import com.xjmall.common.ServerResult;
import com.xjmall.dao.BannerMapper;
import com.xjmall.exception.DataNotFoundException;
import com.xjmall.exception.ParameterCheckException;
import com.xjmall.pojo.Banner;
import com.xjmall.service.IBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 轮播管理业务接口的实现类
 * Created by An on 2017/8/26.
 */
@Service("iBannerService")
public class BannerServiceImpl implements IBannerService {

    @Autowired
    private BannerMapper bannerMapper;
    /**
     * 添加轮播
     * @param banner 待添加的轮播信息
     * @return ServerResult
     */
    @Override
    public ServerResult addBanner(Banner banner) {
        // 校验参数
        if (banner.getProductId() == null || banner.getProductName() == null || banner.getImgUrl() == null) {
            throw new ParameterCheckException("空参数");
        }
        // 查询当前轮播item个数
        int bannerCount = bannerMapper.selectBannerCount();
        if (bannerCount >= 5) {
            // 不能继续添加
            throw new ParameterCheckException("轮播图已达上限，不能添加");
        }
        // 可继续添加
        int rowCount = bannerMapper.insertSelective(banner);
        if (rowCount < 1) {
            throw new DataNotFoundException("添加失败");
        }
        return ServerResult.successMsg("添加成功");
    }

    /**
     * 修改轮播
     * @param banner 待修改的轮播信息
     * @return ServerResult
     */
    @Override
    public ServerResult updateBanner(Banner banner) {
        // 参数校验
        if (banner.getId() == null || banner.getProductId() == null || banner.getProductName() == null || banner.getImgUrl() == null) {
            throw new ParameterCheckException("空参数");
        }
        Banner ban = bannerMapper.selectByPrimaryKey(banner.getId());
        if (ban == null) {
            throw new DataNotFoundException("轮播信息找不到");
        }
        int rowCount = bannerMapper.updateByPrimaryKeySelective(banner);
        if (rowCount < 1) {
            throw new DataNotFoundException("修改失败");
        }
        return ServerResult.successMsg("修改成功");
    }

    /**
     * 根据轮播id获取轮播信息
     * @param id 轮播id
     * @return ServerResult
     */
    @Override
    public ServerResult<Banner> getBannerById(Integer id) {
        // 参数校验
        if (id == null) {
            throw new ParameterCheckException("空参数");
        }
        // 根据轮播id查询轮播信息
        Banner banner = bannerMapper.selectByPrimaryKey(id);
        if (banner == null) {
            throw new DataNotFoundException("找不到该轮播");
        }
        return ServerResult.successData(banner);
    }

    /**
     * 获取轮播列表
     * @return ServerResult
     */
    @Override
    public ServerResult<List<Banner>> getBannerList() {
        List<Banner> bannerList = bannerMapper.selectBannerList();
        return ServerResult.successData(bannerList);
    }

    /**
     * 根据轮播id删除轮播信息
     * @param id 轮播id
     * @return ServerResult
     */
    @Override
    public ServerResult deleteBannerById(Integer id) {
        if (id == null) {
            throw new ParameterCheckException("空参数");
        }
        Banner banner = bannerMapper.selectByPrimaryKey(id);
        if (banner == null) {
            throw new DataNotFoundException("删除失败，该轮播信息不存在");
        }
        int rowCount = bannerMapper.deleteByPrimaryKey(id);
        if (rowCount < 1) {
            throw new DataNotFoundException("删除失败");
        }
        return ServerResult.successMsg("删除成功");
    }
}
